package cn.stylefeng.guns.modular.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.biz.entity.LabMainInfo;
import cn.stylefeng.guns.modular.biz.enums.LabMainInfoExceptionEnum;
import cn.stylefeng.guns.modular.biz.mapper.LabMainInfoMapper;
import cn.stylefeng.guns.modular.biz.pojo.request.LabMainInfoRequest;
import cn.stylefeng.guns.modular.biz.service.LabMainInfoService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Date;

/**
 * MES煤的质检数据业务实现层
 *
 * @author 张朋飞
 * @since 2024/09/29 21:24
 */
@Service
public class LabMainInfoServiceImpl extends ServiceImpl<LabMainInfoMapper, LabMainInfo> implements LabMainInfoService {

	@Override
    public void add(LabMainInfoRequest labMainInfoRequest) {
        LabMainInfo labMainInfo = new LabMainInfo();
        BeanUtil.copyProperties(labMainInfoRequest, labMainInfo);
        this.save(labMainInfo);
    }

    @Override
    public void del(LabMainInfoRequest labMainInfoRequest) {
        LabMainInfo labMainInfo = this.queryLabMainInfo(labMainInfoRequest);
        this.removeById(labMainInfo.getLmiId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(LabMainInfoRequest labMainInfoRequest) {
        this.removeByIds(labMainInfoRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(LabMainInfoRequest labMainInfoRequest) {
        LabMainInfo labMainInfo = this.queryLabMainInfo(labMainInfoRequest);
        BeanUtil.copyProperties(labMainInfoRequest, labMainInfo);
        this.updateById(labMainInfo);
    }

    @Override
    public LabMainInfo detail(LabMainInfoRequest labMainInfoRequest) {
        return this.queryLabMainInfo(labMainInfoRequest);
    }

    @Override
    public PageResult<LabMainInfo> findPage(LabMainInfoRequest labMainInfoRequest) {
        LambdaQueryWrapper<LabMainInfo> wrapper = createWrapper(labMainInfoRequest);
        Page<LabMainInfo> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<LabMainInfo> findList(LabMainInfoRequest labMainInfoRequest) {
        LambdaQueryWrapper<LabMainInfo> wrapper = this.createWrapper(labMainInfoRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 张朋飞
     * @since 2024/09/29 21:24
     */
    private LabMainInfo queryLabMainInfo(LabMainInfoRequest labMainInfoRequest) {
        LabMainInfo labMainInfo = this.getById(labMainInfoRequest.getLmiId());
        if (ObjectUtil.isEmpty(labMainInfo)) {
            throw new ServiceException(LabMainInfoExceptionEnum.LAB_MAIN_INFO_NOT_EXISTED);
        }
        return labMainInfo;
    }

    /**
     * 创建查询wrapper
     *
     * @author 张朋飞
     * @since 2024/09/29 21:24
     */
    private LambdaQueryWrapper<LabMainInfo> createWrapper(LabMainInfoRequest labMainInfoRequest) {
        LambdaQueryWrapper<LabMainInfo> queryWrapper = new LambdaQueryWrapper<>();

        Long lmiId = labMainInfoRequest.getLmiId();
        queryWrapper.eq(ObjectUtil.isNotNull(lmiId), LabMainInfo::getLmiId, lmiId);

        String sampleId = labMainInfoRequest.getSampleId();
        queryWrapper.like(ObjectUtil.isNotEmpty(sampleId), LabMainInfo::getSampleId, sampleId);

        String nak2o = labMainInfoRequest.getNak2o();
        queryWrapper.like(ObjectUtil.isNotEmpty(nak2o), LabMainInfo::getNak2o, nak2o);

        String qgrd = labMainInfoRequest.getQgrd();
        queryWrapper.like(ObjectUtil.isNotEmpty(qgrd), LabMainInfo::getQgrd, qgrd);

        String hgi = labMainInfoRequest.getHgi();
        queryWrapper.like(ObjectUtil.isNotEmpty(hgi), LabMainInfo::getHgi, hgi);

        String caking = labMainInfoRequest.getCaking();
        queryWrapper.like(ObjectUtil.isNotEmpty(caking), LabMainInfo::getCaking, caking);

        String ci = labMainInfoRequest.getCi();
        queryWrapper.like(ObjectUtil.isNotEmpty(ci), LabMainInfo::getCi, ci);

        String coke = labMainInfoRequest.getCoke();
        queryWrapper.like(ObjectUtil.isNotEmpty(coke), LabMainInfo::getCoke, coke);

        String mt = labMainInfoRequest.getMt();
        queryWrapper.like(ObjectUtil.isNotEmpty(mt), LabMainInfo::getMt, mt);

        String std = labMainInfoRequest.getStd();
        queryWrapper.like(ObjectUtil.isNotEmpty(std), LabMainInfo::getStd, std);

        String qnetar = labMainInfoRequest.getQnetar();
        queryWrapper.like(ObjectUtil.isNotEmpty(qnetar), LabMainInfo::getQnetar, qnetar);

        String mad = labMainInfoRequest.getMad();
        queryWrapper.like(ObjectUtil.isNotEmpty(mad), LabMainInfo::getMad, mad);

        String vd = labMainInfoRequest.getVd();
        queryWrapper.like(ObjectUtil.isNotEmpty(vd), LabMainInfo::getVd, vd);

        String ad = labMainInfoRequest.getAd();
        queryWrapper.like(ObjectUtil.isNotEmpty(ad), LabMainInfo::getAd, ad);

        String fcad = labMainInfoRequest.getFcad();
        queryWrapper.like(ObjectUtil.isNotEmpty(fcad), LabMainInfo::getFcad, fcad);

        String had = labMainInfoRequest.getHad();
        queryWrapper.like(ObjectUtil.isNotEmpty(had), LabMainInfo::getHad, had);


        return queryWrapper;
    }

}
